<?php
define('ROOT_DIR', $_SERVER['DOCUMENT_ROOT'].'/');
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
    $file = $_FILES['file'];
    if ($file['error'] === UPLOAD_ERR_OK) {
        if ($file['size'] > 0) {
            $allowedTypes = ['image/png', 'image/jpeg', 'image/gif'];
            if (in_array($file['type'], $allowedTypes)) {
                $newFileName = uniqid('canvas_', true) . '-' . basename($file['name']);
                $uploadDir = ROOT_DIR.'uploads/canvas/'; 
                $uploadPath = $uploadDir . $newFileName;
                if (move_uploaded_file($file['tmp_name'], $uploadPath)) {
                    echo json_encode(array('status' => 200, 'message' => '成功', 'path' => $uploadPath));
                } else {
                    echo json_encode(array('status' => 400, 'message' => '失败'));
                }
            } else {
                echo json_encode(array('status' => 3000, 'message' => '禁止通过该文件类型上传'));
            }
        } else {
            echo json_encode(array('status' => 300, 'message' => '文件大小为0'));
        }
    } else {
        echo json_encode(array('status' => 300, 'message' => '文件上传失败'));
    }
} else {
    echo json_encode(array('status' => 300, 'message' => '非法请求'));
}
?>